package cn.edu.nju.software.zp13.core.nodeDescriptorGenerator.factory;

import java.lang.reflect.Field;

import cn.edu.nju.software.zp13.core.annotation.Attribute;
import cn.edu.nju.software.zp13.core.annotation.Element;
import cn.edu.nju.software.zp13.core.nodeDescriptorGenerator.ArrayComplexNodeDescriptorGenerator;
import cn.edu.nju.software.zp13.core.nodeDescriptorGenerator.AttributeNodeDescriptorGenerator;
import cn.edu.nju.software.zp13.core.nodeDescriptorGenerator.ComplexNodeDescriptorGenerator;
import cn.edu.nju.software.zp13.core.nodeDescriptorGenerator.NodeDescriptorGenerator;
import cn.edu.nju.software.zp13.core.nodeDescriptorGenerator.SimpleElementNodeDescriptorGenerator;
import cn.edu.nju.software.zp13.core.xmlNodeEntity.XmlNodeEntity;

/**
 * A Factory class to create NodeDescriptorGenerator object.
 * 
 * @author Ivan
 *
 */
public class NodeDescriptorGeneratorFactory {
	public static NodeDescriptorGenerator createNodeGenerator(Field f) {
		NodeDescriptorGenerator ndg = null;
		Element elementAnnotation = f.getAnnotation(Element.class);
		Attribute attrAnnotation = f.getAnnotation(Attribute.class);
		if (elementAnnotation == null && attrAnnotation == null) {
			// no valid annotation
		} else if (attrAnnotation != null) { //has attribute
			ndg = new AttributeNodeDescriptorGenerator(attrAnnotation);
		} else if (elementAnnotation != null) { //has element
			Class<?> fieldType = f.getType();
			if (fieldType.isArray()) {
				if (fieldType.getComponentType().getSuperclass() == XmlNodeEntity.class) {
					ndg = new ArrayComplexNodeDescriptorGenerator(elementAnnotation, f);
				}
			} else if (fieldType.getSuperclass() == XmlNodeEntity.class) {
				ndg = new ComplexNodeDescriptorGenerator();
			} else {
				ndg = new SimpleElementNodeDescriptorGenerator(elementAnnotation, f);
			}
		} else {
			// no valid annotations
		}
		return ndg;
	}
}
